package user_assets_ewallet

import (
	"context"
	"errors"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserEWalletInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 用户电子钱包信息 - 查询
func NewUserEWalletInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserEWalletInfoLogic {
	return &UserEWalletInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserEWalletInfoLogic) UserEWalletInfo() (resp *types.UserEWalletsInfoResponse, err error) {
	request := rpc.UserEWalletsRequest{}
	copier.Copy(&request, &types.UserEWalletsInfoRequest{})
	request.UserId = headInfo.GetTokenUid(l.ctx)
	if request.UserId == 0 {
		return &types.UserEWalletsInfoResponse{
			UserEWalletsInfoList: []*types.UserEWalletsInfo{},
		}, errors.New("user is not exist")
	}
	info, err := l.svcCtx.AssetsRpc.UserChannel.UserEWalletsInfo(l.ctx, &request)
	var dataList []*types.UserEWalletsInfo

	if err != nil {
		return &types.UserEWalletsInfoResponse{
			UserEWalletsInfoList: dataList,
		}, err
	}
	if len(info.UserEWalletsInfoList) == 0 {
		return &types.UserEWalletsInfoResponse{
			UserEWalletsInfoList: dataList,
		}, nil
	}
	_ = copier.Copy(&dataList, info.UserEWalletsInfoList)
	for i := 0; i < len(dataList); i++ {

		if info.UserEWalletsInfoList[i].WalletName != "" {
			wallettype, _ := strconv.ParseInt(info.UserEWalletsInfoList[i].WalletName, 10, 32)
			dataList[i].WalletType = int32(wallettype)
		}
	}

	return &types.UserEWalletsInfoResponse{
		UserEWalletsInfoList: dataList,
	}, nil
}
